import { ethers } from "ethers";
import { SignClient } from "@walletconnect/sign-client";
import QRCodeModal from "@walletconnect/qrcode-modal";
const { VITE_INFURA_API_KEY } = import.meta.env;
// 存储WalletConnect实例
let walletConnectInstance: any = null;
const walletConnectOptions = {
  projectId: "81ab4072cd905b3008ec381eef6da04f", // 使用AntDesignWeb3中已有的projectId
  metadata: {
    name: "My Web3 App",
    description: "Connect your wallet to My Web3 App",
    url: "http://localhost:5173",
    icons: ["http://localhost:5173/logo.png"],
  },
};
// 钱包检测规则对象，包含每个钱包的检测函数
export const WALLET_DETECTION_RULES = {
  "MetaMask": {
    detectFn: () => window.ethereum && window.ethereum.isMetaMask,
    getProvider: async() => new ethers.BrowserProvider(window.ethereum),
  },
  "Trust": {
    detectFn: () => window.trustwallet || (window.ethereum && window.ethereum.isTrust),
    getProvider: async() => new ethers.BrowserProvider(window.ethereum),
  },
  "Coinbase": {
    detectFn: () => window.coinbaseWalletExtension,
    getProvider: async() => new ethers.BrowserProvider(window.coinbaseWalletExtension),
  }, 
  "Rabby": {
    detectFn: () => window.rabby || (window.ethereum && window.ethereum.isRabby),
    getProvider: async() => new ethers.BrowserProvider(window.ethereum),
  },
  "imToken": {
    detectFn: () => window.imToken || (window.ethereum && window.ethereum.isImToken),
    getProvider: async () =>  new ethers.BrowserProvider(window.ethereum),
  },
  "OKX": {
    detectFn: () => window.okxwallet || (window.ethereum && window.ethereum.isOKXWallet),
    getProvider: async() => new ethers.BrowserProvider(window.ethereum),
  },
  "BitKeep": {
    detectFn: () => window.bitkeep || (window.ethereum && window.ethereum.isBitKeep),
    getProvider: async () => new ethers.BrowserProvider(window.ethereum),
  },
  "WalletConnect": {
    detectFn: () => true,
    getProvider: async () => {
      if (walletConnectInstance) {
        await walletConnectInstance.disconnect({
          reason: {
            code: 5900,
            message: "Disconnected by user",
          },
        });
      }
      
      // 初始化SignClient
      walletConnectInstance = await SignClient.init(walletConnectOptions);
      
      // 创建会话
      const { uri, approval } = await walletConnectInstance.connect({
        requiredNamespaces: {
          eip155: {
            methods: ["eth_sendTransaction", "eth_signTypedData", "eth_accounts"],
            chains: ["eip155:1", "eip155:137"], // 支持的链
            events: ["chainChanged", "accountsChanged"],
          },
        },
      });
      console.log('==========', uri)
      // 显示QR码
      if (uri) {
        QRCodeModal.open(uri, () => {
          console.log("QR code closed");
        });
      }
      
      // 等待用户批准
      const session = await approval();
      QRCodeModal.close();
      
      console.log("WalletConnect v2连接成功:", session);
      
      // 创建提供者
      const provider = new ethers.BrowserProvider({
        request: async (args: any) => {
          // 通过WalletConnect发送请求
          const { method, params } = args;
          return walletConnectInstance.request({
            topic: session.topic,
            chainId: "eip155:1",
            request: { method, params },
          });
        },
      });
      console.log('======', provider)
      return provider;
    },
  }
} as const;

export type WalletDetectionRule = (typeof WALLET_DETECTION_RULES)[keyof typeof WALLET_DETECTION_RULES];
// 钱包类型枚举，用于类型安全的钱包检测
export type WalletType = keyof typeof WALLET_DETECTION_RULES;

// 运行时的WalletEnum对象，用于验证和遍历钱包类型
export const WalletEnum = {
  ...Object.fromEntries(Object.keys(WALLET_DETECTION_RULES).map(key => [key, key]))
} as Record<WalletType, WalletType>;

// 2. 定义常见网络的 ID 与原生代币符号的映射表（需根据实际需求补充）
export const chainIdToNativeSymbol: Record<string, string> = {
  "0x1": "ETH",    // 以太坊主网
  "0x89": "MATIC", // Polygon 主网
  "0x38": "BNB",   // BSC 主网
  "0xa4b1": "ARB", // Arbitrum 主网
  "0x13881": "MATIC",// Polygon 测试网（Mumbai）
  "0x539": "LETH", // 本地测试网
  "0xaa36a7": "ETH", // 以太坊测试网（Sepolia）
};
export const defaultChain = {
  chainName: "本地测试网络",
  rpcUrls: [`http://127.0.0.1:8545`],
  nativeCurrency: {
    name: "Matic",
    symbol: chainIdToNativeSymbol['0x539'],
    decimals: 18,
  },
  chainId: "0x539"
}
export type ChainItemType = typeof defaultChain
export const chainConfig: Record<string, ChainItemType> = {
  "0x1": {
    chainName: "以太坊主网",
    rpcUrls: [`https://mainnet.infura.io/v3/${VITE_INFURA_API_KEY}`],
    nativeCurrency: {
      name: "Matic",
      symbol: chainIdToNativeSymbol['0x1'],
      decimals: 18,
    },
    chainId: "0x1"
  },
  "0x539": {
    chainName: "本地测试网络",
    rpcUrls: [`http://127.0.0.1:8545`],
    nativeCurrency: {
      name: "Matic",
      symbol: chainIdToNativeSymbol['0x539'],
      decimals: 18,
    },
    chainId: "0x539"
  },
  "0x13881": {
    chainName: "Polygon 测试网（Mumbai）",
    rpcUrls: [`https://rpc.cardona.zkevm-rpc.com`],
    nativeCurrency: {
      name: "Matic",
      symbol: chainIdToNativeSymbol['0x13881'],
      decimals: 18,
    },
    chainId: "0x13881"
  },
  "0xaa36a7": {
    chainName: "Sepolia测试网",
    rpcUrls: [`https://rpc.sepolia.org`],
    nativeCurrency: {
      name: "ETH",
      symbol: chainIdToNativeSymbol['0xaa36a7'],
      decimals: 18,
    },
    chainId: "0xaa36a7"
  },
}
